<template>
	<view class="container">
		<!-- 左侧品牌 -->
		<scroll-view class="scroll-container" scroll-y="true" :scroll-into-view="toView" @scroll="scrollLeft" scroll-with-animation="true">
			<view class="common-text-brand-key">常用品牌</view>
			<scroll-view class="scroll-view-class" :scroll-x="true">
				<view class="scroll-view-class-box">
					<view v-for="(commonItem,index) in commonBrands" :key="index">
						<view class="common-box-brand-view-class" @click="commonBrands_click_item(commonItem)">
							<image class="common-image-brand-view-class" :src="commonItem.cover_url" mode="heightFix"></image>
							<view class="name-text">{{commonItem.name}}</view>
						</view>
					</view>
				</view>
			</scroll-view>

			<view v-for="(keyItem, index1) in brandDataLists" :key="index1" :id="keyItem.key">
				<view class="commonScroll">{{keyItem.key}}</view>
				<view v-for="(brandItem, index2) in keyItem.value" :key="index2" class="brand-item">
					<view class="contact-container" @click="brands_click_item(brandItem)">
						<image class="image-view" :src="brandItem.cover_url" mode="widthFix" />
						<view class="name-text">{{ brandItem.name }}</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<!-- 右侧字母导航条 -->
		<view class="letter-nav">
			<view :class="temp==index?'right-view-text-select':'right-view-text'"
				v-for="(item, index) in brandDataLists" :key="index" @click="toSelectIndex(item.key,index)">
				{{item.key}}
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getCommonBrandsList,
	} from '@/api/brand.js'
	import {
		carsBrandList
	} from '@/api/newCarSerach.js'
	export default {
		data() {
			return {
				temp: 0,
				toView: '',
				commonBrands: [],
				brandDataLists: [],
				brand_type: 'search',
				users_id: '',

				topList: [],
				status:'3'
			}
		},
		onPageScroll(e) {
		},

		onLoad(option) {
			const value = uni.getStorageSync('login')
			if (value) {
				this.users_id = value.id
			}
			getCommonBrandsList(this.users_id, 61).then(res => {
				this.commonBrands = res.data
			})
			carsBrandList().then( res => {
				this.brandDataLists = res.data
			})
		},
		onReady() {
			setTimeout(() => {
				this.getNodesInfo()
			}, 2000)
		},
		methods: {
			getNodesInfo() {
				const query = uni.createSelectorQuery().in(this)
				query.selectAll('.commonScroll').boundingClientRect().exec(data => {
					let res = data[0];
					let rel = [];
					res.map(item => {
						rel.push(item.top);
					});
					this.topList = rel;
				});
			},
			scrollLeft(e) {
				let scrollTop = e.target.scrollTop;
				for (let i = 0; i < this.topList.length; i++) {
					let h1 = this.topList[i];
					let h2 = this.topList[i + 1];
					if (scrollTop >= h1 && scrollTop < h2) {
						this.temp = i;
					}
				}
			},
			toSelectIndex(item, index) {
				this.toView = item
				this.temp = index
			},
			brands_click_item(item) {
				uni.navigateTo({
					url: '/pages/home/newCarSearch/releaseCar/selectBrand/selectCarSeries?name=' + item.name +
						"&brand_id=" + item.id + "&brand_type=" + this.brand_type
				})
			},
			commonBrands_click_item(item) {
				uni.navigateTo({
					url: '/pages/home/newCarSearch/releaseCar/selectBrand/selectCarSeries?name=' + item.name +
						"&brand_id=" + item.id + "&brand_type=" + this.brand_type
				})
			}

		}
	}
</script>

<style scoped lang="scss">
	.container{
		background-color: #F7F8FA;
	}
	.scroll-view-class {
		white-space: nowrap;
		background-color: white;
		height: auto;
	}

	.scroll-view-class-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		height: auto;
	}

	.content-item-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.scroll-container {
		height: 100vh;
	}

	.letter-nav {
		display: flex;
		position: fixed;
		right: 14rpx;
		top: 0;
		height: 100%;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		.right-view-text {
			font-size: 26rpx;
			font-family: DIN Alternate;
			font-weight: bold;
			color: #9B9B9B;
			margin: 10rpx 0;
		}
		
		.right-view-text-select {
			color: #0E59EA;
			font-weight: bold;
			margin: 10rpx 0;
		}
	}


	.common-image-brand-view-class {
		width: auto;
		height: 50rpx;
		background: #FFFFFF;
		opacity: 1;
		border-radius: 22rpx;
		margin: 5rpx;
	}

	.common-text-brand-key {
		font-size: 32rpx;
		font-family: DIN Alternate;
		font-weight: bold;
		color: #1C2125;
		opacity: 1;
		padding: 8rpx 0px 6rpx 28rpx;
		background-color: #F4F4F4;
	}

	.commonScroll {
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #333333;
		padding: 13rpx 29rpx;
	}
	.brand-item{
		padding: 30rpx 29rpx;
		background-color: #fff;
		border-bottom: 1rpx solid #E6E6E6;
		&:last-child{
			border-bottom: none;
		}
		.contact-container {
			display: flex;
			align-items: center;
			.image-view {
				width: 54rpx;
				max-height: 54rpx;
			}
			.name-text{
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
				margin-left: 35rpx;
			}
		}
	}

	.common-box-brand-view-class {
		height: auto;
		width: auto;
		text-align: center;
		margin: 20rpx 0rpx 20rpx 30rpx;
	}
</style>